/***************************************************************************
 *   Copyright (C) 2007 by Leonid Krashenko   *
 *   Leonid.Krashenko@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "frmmain.h"

#include <qpushbutton.h>
#include <qlayout.h>
#include <qapplication.h>
#include <qlistview.h>
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qaction.h>
#include <qtextedit.h>
#include <qpixmap.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
#include <qmessagebox.h>
#include <qstatusbar.h>
#include <qevent.h>
#include <qdragobject.h>
#include <qsettings.h>
#include <qfiledialog.h>

#include "todoitem.h"
#include "todolist.h"

frmMain::frmMain(QWidget *parent, const char *name)
 : QMainWindow(parent, name)
{
	// declaration;
	QPopupMenu *puMenuItem, *puMenuHelp, *puMenuEdit;
	
	QAction *actItemQuit, 
			*actItemNew, 
			*actItemDelete, 
			*actItemSaveList, 
   			*actItemSaveListAs,
			*actItemOpenList,
   			*actItemAdvancedNew, 
			*actEditIncPercent, 
			*actEditDecPercent,	
			*actEditIncPriority, 
			*actEditDecPriority, 
			*actHelpAbout, 
			*actItemOpen, 
			*actItemCopy, 
			*actItemCut, 
			*actItemPaste;
	
	QStatusBar* statusBar;
	
	// creation
	puMenuItem = new QPopupMenu(this);
	puMenuHelp = new QPopupMenu(this);
	puMenuEdit = new QPopupMenu(this);
	
	statusBar = new QStatusBar(this, "frmMain_statusBar");
	
	actItemQuit = new QAction("Quit", "&Quit", CTRL+Key_Q, this, "frmMain_actItemQuit" );
	actItemNew = new QAction("New Task", "&New Task", Key_Insert, this, "frmMain_actItemNew");
	actItemAdvancedNew = new QAction("New Advanced Task", "New &Advanced Task", CTRL+Key_Insert, this, "frmMain_actItemNewAdvanced");
//	actItemToggle = new QAction("Toggle Task", "&Toggle Task", Key_Space, this, "frmMain_actItemNew");
	actItemDelete = new QAction("Delete Task", "&Delete Task", Key_Delete, this, "frmMain_actItemDelete");
	actItemOpenList = new QAction("Open List...", "&Open List", CTRL+Key_O, this, "frmMain_actItemOpenList");
	actItemSaveList = new QAction("Save List...", "&Save List", CTRL+Key_S, this, "frmMain_actItemSaveList");
	actItemSaveListAs = new QAction("Save List As...", "&Save List As...", CTRL+SHIFT+Key_S, this, "frmMain_actItemSaveListAs"); 
	actItemCopy = new QAction("Copy item...", "Copy item", CTRL+Key_C, this, "frmMain_actItemCopy");
	actItemCut = new QAction("Cut item...", "Cut item", CTRL+Key_X, this, "frmMain_actItemCut");
	actItemPaste = new QAction("Paste item...", "Paste item", CTRL+Key_V, this, "frmMain_actItemPaste");
	
	actHelpAbout = new QAction("About", "About", Key_F1, this, "frmMain_actHelpAbout"); 
	
	actEditIncPercent = new QAction("Increase %", "Increase %", Key_Plus, this, "frmMain_actEditIncPercent");
	actEditDecPercent = new QAction("Decrease %", "Decrease %", Key_Minus, this, "frmMain_actEditDecPercent");
	actEditIncPriority = new QAction("Increase !", "Increase !", CTRL+Key_Plus, this, "frmMain_actEditIncPriority");
	actEditDecPriority = new QAction("Decrease !", "Decrease !", CTRL+Key_Minus, this, "frmMain_actEditDecPriority");
	
	_todoList = new ToDoList(this, "frmMain_todoList");
	
	setCentralWidget(_todoList);
	
	menuBar()->insertItem("&Item", puMenuItem);
	menuBar()->insertItem("&Edit", puMenuEdit);
	menuBar()->insertItem("&Help", puMenuHelp);
	
//	puMenuItem->insertItem("&New Item", this, SLOT(addItem()), CTRL+Key_N);
//	puMenuItem->insertItem("&Quit", this, SLOT(quit()), CTRL+Key_Q);
	
	actItemNew->addTo(puMenuItem);
	actItemDelete->addTo(puMenuItem);
	actItemOpenList->addTo(puMenuItem);
	actItemSaveList->addTo(puMenuItem);
	actItemSaveListAs->addTo(puMenuItem);
	actItemQuit->addTo(puMenuItem);
	
	actEditIncPercent->addTo(puMenuEdit);
	actEditDecPercent->addTo(puMenuEdit);
	actEditIncPriority->addTo(puMenuEdit);
	actEditDecPriority->addTo(puMenuEdit);
	actItemCopy->addTo(puMenuEdit);
	actItemCut->addTo(puMenuEdit);
	actItemPaste->addTo(puMenuEdit);
	
	actHelpAbout->addTo(puMenuHelp);
	
	// setup connections
	connect ( actItemNew, SIGNAL(activated()), _todoList, SLOT(addItem()) );
	connect ( actItemAdvancedNew, SIGNAL(activated()), _todoList, SLOT(addAdvancedItem()) );	
	connect ( actItemDelete, SIGNAL(activated()), _todoList, SLOT(deleteItem()) );
	connect ( actItemQuit, SIGNAL(activated()), qApp, SLOT(quit()) );
	connect ( actItemOpenList, SIGNAL(activated()), SLOT(openToDo()) );
	connect ( actItemSaveList, SIGNAL(activated()), SLOT(saveToDo()) );
	connect ( actItemSaveListAs, SIGNAL(activated()), SLOT(saveAs()) );
	//connect ( actItemCopy, SIGNAL(activated()), _todoList, SLOT(copyItem()) );
	connect ( actItemCut, SIGNAL(activated()), _todoList, SLOT(cutItem()) );
	connect ( actItemPaste, SIGNAL(activated()), _todoList, SLOT(pasteItem()) );
	
	connect ( actEditIncPercent, SIGNAL(activated()), _todoList, SLOT(incCompletion()) );
	connect ( actEditDecPercent, SIGNAL(activated()), _todoList, SLOT(decCompletion()) );
	connect ( actEditIncPriority, SIGNAL(activated()), _todoList, SLOT(incPriority()) );
	connect ( actEditDecPriority, SIGNAL(activated()), _todoList, SLOT(decPriority()) );
	
	connect ( actHelpAbout, SIGNAL(activated()), SLOT(about()) );
	
	// read settings
	this->readSettings();
	
	// autoload previous
	this->reOpenToDo();
}

// void frmMain::itemClicked(QListViewItem* item)
// {
// 	statusBar()->clear();
// 	
// 	if(!item){
// 		statusBar()->message("No item selected");
// 		return;
// 	}
// 	
// 	QString strItemInfo = "% = " + QString::number(((ToDoItem*)item)->getPercentComplete());
// 	statusBar()->message(strItemInfo);
// }

void frmMain::about()
{
	QMessageBox::information(this, "myToDo 0.1 pre alpha", "L.S.Krashenko \ne-mail: Leonid.Krashenko@gmail.com");
}

void frmMain::saveToDo()
{
	if(_taskListPath=="")
	{
		_taskListPath = QFileDialog::getSaveFileName("/home", "XML files (*.xml)", this, "open file dialog", "Choose a file to save" );
	}
	
	if(_taskListPath=="")
		return;
		
	if(_todoList->saveToXMLFile(_taskListPath))
		statusBar()->message("...saved :)", 2000);
	else 
		statusBar()->message("error!");
}

void frmMain::reOpenToDo()
{
	if(_taskListPath!=""){
		if(_todoList->loadFromXMLFile(_taskListPath))
			statusBar()->message("...loaded :)", 2000);
		else
			statusBar()->message("error!");
	}
}

void frmMain::saveAs()
{
	QString file = QFileDialog::getSaveFileName("/home", "XML files (*.xml)", this, "open file dialog", "Choose a file to save" );
	if(file == "")
		return;
	
	_taskListPath = file;
	
	saveToDo();
}

void frmMain::openToDo()
{
	QString file = QFileDialog::getOpenFileName("/home", "XML files (*.xml)", this, "open file dialog", "Choose a file to open" );
	if(file!="")
	{
		_taskListPath = file;
		this->reOpenToDo();
	}
}

void frmMain::dragEnterEvent(QDragEnterEvent* e)
{
	
}

void frmMain::dropEvent(QDropEvent* e)
{
	
}

// void frmMain::addItem()
// {
// 	
// 	//QCheckListItem *item = new QCheckListItem(_lstItems, "new item", QCheckListItem::CheckBox);
// 	ToDoItem* item = new ToDoItem(_lstItems, "new todo item");
// 	item->setRenameEnabled(0, true);
// 	_lstItems->insertItem(item);
// 	
// }

frmMain::~frmMain()
{
	this->writeSettings();
}

void frmMain::readSettings()
{
	bool ok=true;
	
	QSettings settings;
	settings.setPath("neiderra.by.ru", "KaKaDoo");
	settings.beginGroup("/MainWindow");
		int width = settings.readNumEntry("/geometry/width", 380, &ok);
		int height = settings.readNumEntry("/geometry/height", 400, &ok);
		int x = settings.readNumEntry("/geometry/x",0,&ok);
		int y = settings.readNumEntry("/geometry/y",0,&ok);
	settings.endGroup();
	settings.beginGroup("/Application");
		_taskListPath = settings.readEntry("/tasklist/lastOpened", "", &ok);
	settings.endGroup();
	
	this->resize(width, height);
	this->move(x, y);
}

void frmMain::writeSettings()
{
	QSettings settings;
	settings.setPath("neiderra.by.ru", "KaKaDoo");
	settings.beginGroup("/MainWindow");
		settings.writeEntry("/geometry/width", width());
		settings.writeEntry("/geometry/height", height());
		settings.writeEntry("/geometry/x", x());
		settings.writeEntry("/geometry/y", y());
	settings.endGroup();
	settings.beginGroup("/Application");
		settings.writeEntry("/tasklist/lastOpened", _taskListPath);
	settings.endGroup();
}
